package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class ForgetPassWordActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText Et_UserName,Et_AcNum,Et_TelPhone,Et_NewPassWord,Et_reNewPassWord,Et_Problem,Et_Answer;
    private Button Btn_Yes,Btn_No;
    private int id;
    private String Problem;
    private String Answer;
    private String UserName;
    private String AcNum;
    private String Telphone;

    private static final String TAG = "ForgetPassWordActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_pass_word);

        Et_UserName = findViewById(R.id.Et_UserName);
        Et_AcNum = findViewById(R.id.Et_AcNum);
        Et_TelPhone = findViewById(R.id.Et_TelPhone);
        Et_NewPassWord = findViewById(R.id.Et_NewPassWord);
        Et_Problem = findViewById(R.id.Et_Problem);
        Et_reNewPassWord = findViewById(R.id.Et_reNewPassWord);
        Et_Answer = findViewById(R.id.Et_Answer);

        Btn_Yes = findViewById(R.id.Btn_Yes);
        Btn_No = findViewById(R.id.Btn_No);

        Btn_Yes.setOnClickListener(this);
        Btn_No.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.Btn_Yes:
                find();
                break;
            case R.id.Btn_No:
                Intent intent = new Intent(ForgetPassWordActivity.this, LoginActivity.class);
                startActivity(intent);
                break;
        }
    }



    private void find() {
        final String J_name= Et_UserName.getText().toString();
        final String J_AcNum = Et_AcNum.getText().toString();
        final String J_password = Et_NewPassWord.getText().toString();
        final String J_rePassWord = Et_reNewPassWord.getText().toString();
        final String J_TelPhone = Et_TelPhone.getText().toString();
        final String J_Problem = Et_Problem.getText().toString();
        final String J_Answer = Et_Answer.getText().toString();


        if(!J_password.equals(J_rePassWord)){
            Toast.makeText(this, "两次输入的密码不正确！", Toast.LENGTH_SHORT).show();
            return;
        }
        if (J_name.equals("")||J_AcNum.equals("")||J_password.equals("")||J_TelPhone.equals("")||
                J_Problem.equals("")||J_Answer.equals("")){
            Toast.makeText(this, "输入的数据不能为空！", Toast.LENGTH_SHORT).show();
            return;
        }

        new Thread(new Runnable() {
            private static final String TAG = "MainActivity";
            @Override
            public void run() {
                //1 client对象
                OkHttpClient client = new OkHttpClient();
                //2 Request对象
                FormBody body = new FormBody.Builder()

                        .build();
                Request request = new Request.Builder()
                        .url("http://10.2.64.68:8080/juyuan/users/list?accountNumber="+Et_AcNum.getText().toString() )
                        .post(body)
                        .build();
                //3 call对象
                Call call = client.newCall(request);
                //4 执行call对象得到数据
                try {
                    Response response = call.execute();
                    String data = response.body().string();
                    Gson gson = new Gson();
                    LoginGson gu = gson.fromJson(data, LoginGson.class);
                    id = gu.getId();
                    Problem = gu.getProblem();
                    Answer = gu.getAnswer();
                    UserName = gu.getUserName();
                    AcNum = gu.getAccountNumber();
                    Telphone = gu.getTelphone();

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Log.e(TAG, "run: "+ Problem + "***" + Answer + "***" + UserName + "***"+ Telphone);
                            Log.e(TAG, "run: "+ J_Problem + "***" + J_Answer + "***" + J_name + "***"+ J_TelPhone);
                            if (! UserName.equals(J_name)|| !AcNum.equals(J_AcNum)){
                                Toast.makeText(ForgetPassWordActivity.this, "用户名或账号输入不正确！", Toast.LENGTH_SHORT).show();
                            }
                            if (!Telphone.equals(J_TelPhone)){
                                Toast.makeText(ForgetPassWordActivity.this, "电话号码输入不正确！", Toast.LENGTH_SHORT).show();
                            }

                            if (!J_password.equals(J_rePassWord)){
                                Toast.makeText(ForgetPassWordActivity.this, "两次输入的密码不正确！", Toast.LENGTH_SHORT).show();
                            }

                            if (Problem.equals(J_Problem) && Answer.equals(J_Answer) && UserName.equals(J_name)
                                    && AcNum.equals(J_AcNum) && Telphone.equals(J_TelPhone) && J_password.equals(J_rePassWord)){
                                updata();
                            }else{
                                Toast.makeText(ForgetPassWordActivity.this, "密保问题或密保答案不正确！", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }).start();

    }

    private void updata() {
        final String J_name= Et_UserName.getText().toString();
        final String J_AcNum = Et_AcNum.getText().toString();
        final String J_password = Et_NewPassWord.getText().toString();
        final String J_rePassWord = Et_reNewPassWord.getText().toString();
        final String J_TelPhone = Et_TelPhone.getText().toString();
        final String J_Problem = Et_Problem.getText().toString();
        final String J_Answer = Et_Answer.getText().toString();


        new Thread(new Runnable() {
            private static final String TAG = "MainActivity";

            @Override
            public void run() {
                //1 client对象
                OkHttpClient client = new OkHttpClient();
                //2 Request对象(参数1：数据类型 参数2传递的json串）
                JSONObject object = new JSONObject();

                try {
                    object.put("id",id);
                    object.put("accountNumber", J_AcNum);
                    object.put("answer", J_Answer);
                    object.put("passWord", J_password);
                    object.put("problem", J_Problem);
                    object.put("telphone", J_TelPhone);
                    object.put("userName", J_name);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String json = object.toString();
                RequestBody body = FormBody.create(MediaType.parse("application/json:charset=utf-8"), json);
                //构建一个Request，将FormBody作为Post方法的参数传入，

                    Request request = new Request.Builder()
                            .url("http://10.2.64.68:8080/juyuan/users/updateUser?id="+id+"&userName="
                                    +J_name+"&accountNumber="+J_AcNum+"&passWord="+J_password+"&telphone="+J_TelPhone+"&problem="+J_Problem+"&answer="+J_Answer)
                            .post(body)
                            .build();
                    //3 call对象
                    Call call = client.newCall(request);
                    //调度Call对象
                    call.enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            Log.e(TAG, "onFailure: " + e);
                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            Log.e(TAG, "PostJson: " + response.body().string());

                        }
                    });


            }

        }).start();
        Toast.makeText(ForgetPassWordActivity.this, "新密码设置成功！", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(ForgetPassWordActivity.this, LoginActivity.class);
        startActivity(intent);
    }
    }

